package com.zds.demo;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class SwitchActivity extends AppCompatActivity {

    @SuppressLint("UseSwitchCompatOrMaterialCode")
    private Switch switch1, switch2;
    private TextView text1, text2;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switch);

        // 初始化Switch控件
        switch1 = findViewById(R.id.switch1);
        switch2 = findViewById(R.id.switch2);
        text1 = findViewById(R.id.text1);
        text2 = findViewById(R.id.text2);

        text1.setOnClickListener(v -> {
            switch1.setChecked(!switch1.isChecked());
        });

        text2.setOnClickListener(v -> {
            switch2.setChecked(!switch2.isChecked());
        });
        // 设置Switch1的监听器
        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    String message = "变道预警已开启";
                    Toast.makeText(SwitchActivity.this, message, Toast.LENGTH_SHORT).show();
                }
            }
        });

        // 设置Switch2的监听器
        switch2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {
                    String message = "开门预警已开启";
                    Toast.makeText(SwitchActivity.this, message, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}